//
//  OfficialNewViewController.m
//  LeagueOfLegend
//
//  Created by appying_wxj on 14-7-4.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "OfficialNewViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "DetailsViewController.h"
#import "OfficialTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "Request.h"
#import "UIImageView+ASIRequest.h"
#import "MobClick.h"
@interface OfficialNewViewController ()
{
    NSMutableArray *_titleArray;
    
}

@end

@implementation OfficialNewViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, 320, 0.5)];
//    imageView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:imageView];
    _page = 1;

    
//    if (version>=7){
//        imageView.frame = CGRectMake(0, 64, 320, 0.5);
//    }else{
//        imageView.frame = CGRectMake(0, 24, 320, 0.5);
//    }
}
- (void)viewWillAppear:(BOOL)animated {
    [self creatHeard];
    [self creatfindObjects];
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"OfficialNewViewController"];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"OfficialNewViewController"];
}

//创建控件
-(void)creatHeard
{
    self.titleLab.text = @"周更新内容";
    //_officialTaleView = [[PullingRefreshTableView alloc] initWithFrame:CGRectMake(0, 64.5, 320, self.view.frame.size.height - 120) pullingDelegate:self];
    
    
    if (version>=7){
        _officialTaleView = [[PullingRefreshTableView alloc] initWithFrame:CGRectMake(0, 64.5, 320, self.view.frame.size.height - 120) pullingDelegate:self];
        //_officialTaleView.frame = CGRectMake(0, 64.5, 320, self.view.frame.size.height - 120);
    }else{
        _officialTaleView = [[PullingRefreshTableView alloc] initWithFrame:CGRectMake(0, 24.5, 320, self.view.frame.size.height - 95) pullingDelegate:self];
       // _officialTaleView.frame = CGRectMake(0, 24.5, 320, self.view.frame.size.height - 95);
    }
    _officialTaleView.delegate = self;
    _officialTaleView.dataSource = self;
    _officialTaleView.rowHeight = 44;
    _officialTaleView.sectionHeaderHeight = 44;
    // _officialTaleView.sectionIndexBackgroundColor = [UIColor redColor];
    //_officialTaleView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [_officialTaleView setHeaderOnly:YES];//只有下拉刷新
    [self.view addSubview:_officialTaleView];
    //[_officialTaleView launchRefreshing];

}

//查询 获取数据
-(void)creatfindObjects
{
    if ([Request checkNet]) {
        _titleArray = [[NSMutableArray alloc] initWithCapacity:0];
        _heroImageArray = [[NSMutableArray alloc] initWithCapacity:0];
        _heroNameArray = [[NSMutableArray alloc] initWithCapacity:0];
        _heroContentArray = [[NSMutableArray alloc] initWithCapacity:0];
        AVQuery *query = [AVQuery queryWithClassName:@"summary"];
        AVObject *summary = [query getObjectWithId:@"53bba3b4e4b08a6e7998445b"];
        _playerName = [summary objectForKey:@"title"];//请求区头上面的信息
        _contentString = [summary objectForKey:@"content"];
        AVRelation *detail = [summary relationforKey:@"detailId"];
        
        [[detail query] findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (error) {
                
            } else {
                _titleArray = [NSMutableArray arrayWithArray:objects];
                for (int i = 0; i < _titleArray.count; i++) {
                    AVObject *hero = [_titleArray objectAtIndex:i];
                    [_heroImageArray addObject:[hero objectForKey:@"heroImageUrl"]];
                    [_heroNameArray addObject:[hero objectForKey:@"title"]];
                    [_heroContentArray addObject:[hero objectForKey:@"content"]];
                    
                }
                [_officialTaleView reloadData];
                
                [self.officialTaleView tableViewDidFinishedLoadingWithMessage:@"加载完成"];
                
            }
        }];
        
    }else{
        [self.officialTaleView removeFromSuperview];
        
    }
}

#pragma mark--ButtonClick
-(void)goToNextButtonClick:(UIButton *)sender{
    DetailsViewController *detailVC = [[DetailsViewController alloc] init];
    detailVC.textForContext = _contentString;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:NO];
    [MobClick event:@"OfficialNewVCGoToNextButton"];
}
//下拉刷新
- (void)pullingTableViewDidStartRefreshing:(PullingRefreshTableView *)tableView
{
    //将刷新设置为yes，在加载数据前判断是否刷新来对数据进行一些操作
    self.refreshing = YES;
    [self performSelector:@selector(creatfindObjects) withObject:nil afterDelay:1.f];
    [MobClick event:@"OfficialNewVCpulling"];
}


#pragma mark--tableView
// 设置有多少个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
// 自定义区头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    // 区头的背景
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    view.backgroundColor = [UIColor whiteColor];
    
    //  区头上面的label
    UILabel *timeLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 24)];
    timeLab.font = [UIFont systemFontOfSize:14];
    timeLab.text = [NSString stringWithFormat:@"版本更新时间：%@",_playerName];
    [view addSubview:timeLab];
    
    // 区头底部的一条线
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43.5, 320, 0.5)];
    lineImage.backgroundColor = [UIColor colorWithRed:0.82 green:0.82 blue:0.84 alpha:1];
    [view addSubview:lineImage];
    
    // 区头上的按钮
    UIButton *allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    allBtn.frame = CGRectMake(0, 0, 320, 44);
    allBtn.backgroundColor = [UIColor clearColor];
    [allBtn addTarget:self action:@selector(goToNextButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:allBtn];
    return view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_titleArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    OfficialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[OfficialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    UIView *view_bg = [[UIView alloc]initWithFrame:CGRectMake(20, 64, 320, 560-64-49)];
    view_bg.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    cell.selectedBackgroundView = view_bg;
    
    // 英雄的名字
    cell.titleLabel.text = [_heroNameArray objectAtIndex:indexPath.row];
    
    // 英雄的图片
    NSURL *imageUrl = [NSURL URLWithString:[_heroImageArray objectAtIndex:indexPath.row]];
    
    [cell.heroImage setImageWithURL:imageUrl];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *detailsView = [[DetailsViewController alloc] init];
    detailsView.textForContext = [_heroContentArray objectAtIndex:indexPath.row];
    detailsView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailsView animated:NO];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - ScrollView Method
//手指开始拖动方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.officialTaleView tableViewDidScroll:scrollView];
}

//手指结束拖动方法
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.officialTaleView tableViewDidEndDragging:scrollView];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
